﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Caliburn.Castle;
using Caliburn.Core;
using Caliburn.PresentationFramework;
using Caliburn.PresentationFramework.ApplicationModel;
using Caliburn.Unity;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using CI.Models.Data;
using CI.Models.Production;
using CI.Models.Production.Interfaces;
using CI.Services;
using CI.Utility;
using CI.ViewModels;
using CI.ViewModels.Interfaces;
using CI.Views;
using Microsoft.Practices.ServiceLocation;

namespace CI
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        private WindsorContainer _container;

        protected override void ConfigurePresentationFramework(PresentationFrameworkModule module)
        {
            module.UsingViewStrategy<InterfaceViewStrategy>();
        }

        protected override IServiceLocator CreateContainer()
        {
            _container = new WindsorContainer();

            ConfigureWindsor();

            return new WindsorAdapter(_container);
        }


        protected override object CreateRootModel()
        {
            return _container.Resolve<IShellViewModel>();
        }

        private void ConfigureWindsor()
        {
            LoadProductionRepository();

            var diagrammingService = new DiagrammingService();

            _container.Register(Component.For<IShellViewModel>().ImplementedBy<ShellViewModel>().LifeStyle.Singleton);
            _container.Register(Component.For<ISheetsViewModel>().ImplementedBy<SheetsViewModel>().LifeStyle.Singleton);
            _container.Register(Component.For<IOwnerService>().ImplementedBy<TestOwnerService>().LifeStyle.Singleton);

            _container.Register(Component.For<IDiagrammingServiceViewSide, IDiagrammingServiceViewModelSide>().Instance(diagrammingService).LifeStyle.Singleton);
        }

        private void LoadProductionRepository()
        {
            var sri = GetResourceStream(new Uri("Resources/XmlData/production.xml", UriKind.Relative));

            if (sri != null)
                using (var s = sri.Stream)
                {
                    var itemRepo = new XmlDataRepository<Item>(s);
                    itemRepo.Load();
                    _container.Register(Component.For<IStaticDataRepository<Item>>().Instance(itemRepo).LifeStyle.Singleton);
                }
        }
    }

    
}
